package com.example.happ.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.example.happ.mapper.BlacklistInfoMapper;
import com.example.happ.pojo.Blacklist;
import com.example.happ.service.BlacklistInfoService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;

@Service
public class BlacklistInfoServiceImpl implements BlacklistInfoService {
    @Resource
    private BlacklistInfoMapper blacklistInfoMapper;
    @Override
    public Boolean addBlacklists(Map<String, String> requestBody) {
        Blacklist blacklist = new Blacklist();
        blacklist.setUserId(Integer.valueOf(requestBody.get("userId")));
        blacklist.setMerchantId(Integer.valueOf(requestBody.get("merchantId")));
        blacklist.setReason(requestBody.get("reason"));
        blacklist.setBlacklistDate(new Date());
        return  blacklistInfoMapper.insert(blacklist)>0;
    }

    @Override
    public Map<String, Object> findBlacklistsByMerchantId(Map<String, String> requestBody) {
        System.out.println("黑名单列表请求收到的数据:"+requestBody);
        int page = Integer.parseInt(requestBody.get("page"));
        int size = Integer.parseInt(requestBody.get("size"));
        Page<Blacklist> pageRequest = new Page<>(page, size);
        QueryWrapper<Blacklist> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("merchant_id", requestBody.get("merchantId"));
        Page<Blacklist> pageResult = blacklistInfoMapper.selectPage(pageRequest, queryWrapper);
        Map<String, Object> result = new HashMap<>();
        result.put("data", pageResult.getRecords());     // 当前页数据
        result.put("total", pageResult.getTotal());      // 总记录数
        result.put("currentPage", pageResult.getCurrent()); // 当前页码
        result.put("pageSize", pageResult.getSize());
        System.out.println("黑名单列表请求返回的数据:"+result);
        return result;
    }

    @Override
    public boolean deleteBlacklistsByMerchantId(Map<String, String> requestBody) {
        QueryWrapper<Blacklist> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("blacklist_id", requestBody.get("blacklistId"));
        return blacklistInfoMapper.delete(queryWrapper)>0;
    }
}
